﻿using MySql.Data.MySqlClient;
using SmartRFIDSystem19.dao;
using System;
using System.Windows.Forms;

namespace SmartRFIDSystem.dao
{
    public class UserDao
    {

        public int register(string userName, string passWord, string email)
        {
            //数据库连接
            Connection.Connect();

            string sqlStr = "insert into user (id,name,password,isAdmin,email) values (@id,@name,@password,@power,@email)";
            Connection.comm = new MySqlCommand(sqlStr, Connection.conn);
            Connection.comm.Parameters.Add(new MySqlParameter("@id", null));
            Connection.comm.Parameters.Add(new MySqlParameter("@name", userName));
            Connection.comm.Parameters.Add(new MySqlParameter("@password", passWord));
            Connection.comm.Parameters.Add(new MySqlParameter("@power", "商铺"));
            Connection.comm.Parameters.Add(new MySqlParameter("@email", email));
            try
            {
                int r = Connection.comm.ExecuteNonQuery();
                if (r == 1)
                {
                    MessageBox.Show("注册成功");
                    return 1;
                }
                else
                {
                    MessageBox.Show("注册失败");
                    return -1;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("注册失败");
                return -1;
            }
            finally
            {
                if (Connection.conn != null)
                    Connection.conn.Close();
            }

            //关闭连接
            return 0;
        }
        public int login(string userName, string passWord, string power)
        {

            //数据库连接
            try
            {
                Connection.conn = new MySqlConnection(Connection.M_str_sqlcon);
                Connection.conn.Open();
                // MessageBox.Show("成功");

            }
            catch
            {

            }

            string sqlStr = "select*from user where name=@name and password=@password and isAdmin=@power";
            Connection.comm = new MySqlCommand(sqlStr, Connection.conn);
            Connection.comm.Parameters.Add(new MySqlParameter("@name", userName));
            Connection.comm.Parameters.Add(new MySqlParameter("@password", passWord));
            Connection.comm.Parameters.Add(new MySqlParameter("@power", power));
            try
            {
                object id = Connection.comm.ExecuteScalar();
                if (id == null)
                {
                    MessageBox.Show("用户名密码错误");
                    return -1;
                }
                else
                {



                    if (power.Equals("管理员"))
                    {
                        return 1;
                    }
                    else if (power.Equals("商铺"))
                    {
                        return 2;
                    }
                }
            }
            catch
            {
                MessageBox.Show("用户名密码错误！");
                return -1;
            }

            finally
            {
                if (Connection.conn != null)
                    Connection.conn.Close();
            }

            //关闭连接

            return 0;
        }

        public int initUser(string mfid,string name)
        {

            //数据库连接
            try
            {
                Connection.conn = new MySqlConnection(Connection.M_str_sqlcon);
                Connection.conn.Open();
                 //MessageBox.Show("成功");

            }
            catch
            {

            }

            string sqlStr = "select*from account where mfid = @mfid";
            Connection.comm = new MySqlCommand(sqlStr, Connection.conn);
            Connection.comm.Parameters.Add(new MySqlParameter("@mfid", mfid));
            try
            {
                object id = Connection.comm.ExecuteScalar();
                if (id == null)
                {
                    string sql = "insert into account (mfid,balance,name,class,remark) values (@mfid,@balance,@name,@class,@remark)";
                    Connection.comm = new MySqlCommand(sql, Connection.conn);
                    Connection.comm.Parameters.Add(new MySqlParameter("@mfid", mfid));
                    float a = 0;
                    Connection.comm.Parameters.Add(new MySqlParameter("@balance", a));
                    Connection.comm.Parameters.Add(new MySqlParameter("@name", name));
                    Connection.comm.Parameters.Add(new MySqlParameter("@class", "无"));
                    Connection.comm.Parameters.Add(new MySqlParameter("@remark", "无"));
                    int r = Connection.comm.ExecuteNonQuery();
                    if (r == 1)
                    {
                        MessageBox.Show("初始化成功");
                        return 1;
                    }
                    else
                    {
                        MessageBox.Show("初始化失败");
                        return -1;
                    }
                   
                }
                else
                {
                    string sql = "update account set balance=@balance,name=@name where mfid=@mfid";
                    Connection.comm = new MySqlCommand(sql, Connection.conn);
                    float a = 0;
                    Connection.comm.Parameters.Add(new MySqlParameter("@balance", a));
                    Connection.comm.Parameters.Add(new MySqlParameter("@name", name));
                    Connection.comm.Parameters.Add(new MySqlParameter("@mfid", mfid));
                    int r = Connection.comm.ExecuteNonQuery();
                    if (r == 1)
                    {
                        MessageBox.Show("初始化成功");
                        return 1;
                    }
                    else
                    {
                        MessageBox.Show("初始化失败");
                        return -1;
                    }
                }
           }
            catch
            {
                //MessageBox.Show("初始化失败!");
               // return -1;
            }

           // finally
            {
                if (Connection.conn != null)
                    Connection.conn.Close();
            }

            //关闭连接

            return 0;
        }
        public int changemoney(string mfid,float a)
        {

            //数据库连接
            try
            {
                Connection.conn = new MySqlConnection(Connection.M_str_sqlcon);
                Connection.conn.Open();
               // MessageBox.Show("成功");

            }
            catch
            {

            }

            try { 
                    string sql = "update account set balance=@balance where mfid=@mfid";
                    Connection.comm = new MySqlCommand(sql, Connection.conn);
                    Connection.comm.Parameters.Add(new MySqlParameter("@balance", a));
                    Connection.comm.Parameters.Add(new MySqlParameter("@mfid", mfid));
                    int r = Connection.comm.ExecuteNonQuery();
                    if (r == 1)
                    {
                        MessageBox.Show("成功");
                        return 1;
                    }
                    else
                    {
                        MessageBox.Show("失败");
                        return -1;
                    }
                }
            
            catch
            {
                //MessageBox.Show("初始化失败!");
                // return -1;
            }

             finally
            {
                if (Connection.conn != null)
                    Connection.conn.Close();
            }

            //关闭连接

            return 0;
        }
        public int ChangePassword(string userName, string passWord, string email)
        {
            //数据库连接
            try
            {
                Connection.conn = new MySqlConnection(Connection.M_str_sqlcon);
                Connection.conn.Open();
                // MessageBox.Show("成功");

            }
            catch
            {

            }

            string sqlStr = "update user set password=@password where name=@name and email=@email";
            Connection.comm = new MySqlCommand(sqlStr, Connection.conn);
            Connection.comm.Parameters.Add(new MySqlParameter("@name", userName));
            Connection.comm.Parameters.Add(new MySqlParameter("@password", passWord));
            Connection.comm.Parameters.Add(new MySqlParameter("@email", email));
            try
            {
                int r = Connection.comm.ExecuteNonQuery();
                if (r == 1)
                {
                    MessageBox.Show("修改成功");
                    return 1;
                }
                else
                {
                    MessageBox.Show("修改失败");
                    return -1;
                }
            }
            catch (Exception ex)
            {
                //MessageBox.Show("修改失败");
                return -1;
            }
            finally
            {
                if (Connection.conn != null)
                    Connection.conn.Close();
            }
        }

    }
}
